#include "sys/stat.h"
#include <stdio.h> 
#include <linux/types.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/ioctl.h> 
#include <errno.h> 
#include <assert.h> 
#include <string.h> 
#include <linux/i2c.h> 
#include <linux/i2c-dev.h>
 
//#include "rc522_device.h"
/***************************************************************
文件名 : drive_read_app.c
作者 : jiaozhu
版本 : V1.0
描述 : 驱动读取测试
其他 : 使用方法：./drive_read_app [/dev/xxx]
argv[1] 需要读取的驱动
日志 : 初版 V1.0 2023/1/4
***************************************************************/
 
/**
* @brief main 主程序
* @param argc argv 数组元素个数
* @param argv 具体参数
* @return 0 成功;其他 失败
*/
int main(int argc, char *argv[])
{
    int fd;
    char *filename;
    unsigned char card_buf[16];
    unsigned char card_id[16];
    unsigned char write_buf[2];
    int value[18];
    int cmd;
    int ret = 0;
 
    if(argc != 2){
        printf("Error Usage!\r\n");
        return -1;
    }
 
    filename = argv[1];
 
    /* 打开驱动文件 */
    fd = open(filename, O_RDWR);
    if(!fd){
        printf("Can't open file %s\r\n", filename);
        return -1;
    }
 
    close(fd);
 
    return 0;
 
    /* 设置卡片密码 */
    lseek(fd, 66*16, SEEK_SET);
    memset(card_buf, 0xFF, sizeof(card_buf));
    ret = write(fd, card_buf, sizeof(card_buf));
    if(ret < 0){
        printf("Failed to set integration time...........!\r\n");
    }
    else
    {
    	printf("set password ret %d!\r\n",ret);
    }
    /* 获取卡片类型 */
    //sleep(1);
    lseek(fd, 64*16, SEEK_SET);
    ret = read(fd, card_buf, sizeof(card_buf));
    if (ret == 0)       
    {
        printf("Card type is : (0x%4x)\n", (card_buf[0] << 8) | card_buf[1]);
    }
    else
    {
        printf("read file %s failed ret %d!\r\n", filename,ret);
    }
 
 
    /* 获取卡片id */
    //sleep(1);
    lseek(fd, 65*16, SEEK_SET);
    ret = read(fd, card_id, sizeof(card_id));
    if (ret == 0)       
    {
        printf("card id is : %02x%02x%02x%02x\n", card_id[0], card_id[1], card_id[2], card_id[3]);
    }
    else
    {
        printf("read file %s failed!\r\n", filename);
    }
 
    /* 选择卡片 */
    //sleep(1);
    lseek(fd, 65*16, SEEK_SET);
    ret = write(fd, card_id, sizeof(card_id));
    if(ret < 0){
        printf("Failed to select card!\r\n");
    }
 
    /* 写数据 */
    //sleep(1);
    lseek(fd, 4*16, SEEK_SET);
    memset(card_buf, 0x58, sizeof(card_buf));
    ret = write(fd, card_buf, sizeof(card_buf));
    if(ret < 0){
    	printf("Failed to write card block information\r\n");
    }
 
 
    /* 获取卡片块数据 */
    sleep(1);
    lseek(fd, 0*16, SEEK_SET);
    ret = read(fd, card_buf, sizeof(card_buf));
    if (ret == 0)       
    {
        for (int i = 0; i < 16; i++)
        {
            printf("%02x ", card_buf[i]);
        }
        printf("\r\n ");
    }
    else
    {
        printf("Failed to read card block information");
    }
 
    //sleep(1);
    close(fd);
 
    return 0;
}
 
 
